//
//  BSAdViewController.m
//  Baisijie
//
//  Created by 王建伟 on 2017/3/3.
//  Copyright © 2017年 RangGuangyu. All rights reserved.
//

#import "BSAdViewController.h"
#import "BSTabBarController.h"
#import <AFNetworking.h>
#import <UIImageView+WebCache.h>
#import "BSAdItem.h"
#import <MJExtension.h>

@interface BSAdViewController ()


@property (weak, nonatomic) IBOutlet UIImageView *bgImageView;
@property (weak, nonatomic) IBOutlet UIView *adContentView;
@property (weak, nonatomic) IBOutlet UIButton *adTimeBtn;
/** 动画层 */
@property (nonatomic ,strong) CAShapeLayer *layer;
/** 计时器 */
@property (nonatomic ,weak) NSTimer *timer;
/** 广告时间 */
@property (nonatomic ,assign) NSInteger second;
/** 广告图片 */
@property (nonatomic ,weak) UIImageView *imageView;
/** 广告模型 */
@property (nonatomic ,strong) BSAdItem *adItem;

@end

@implementation BSAdViewController

- (UIImageView *)imageView {
    if (!_imageView) {
        UIImageView *imageView = [[UIImageView alloc] init];
        [self.adContentView addSubview:imageView];
        imageView.userInteractionEnabled = YES;
        [imageView addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tap)]];
        _imageView = imageView;
    }
    return _imageView;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    [self setupLaunchImage];
    [self getAdInfo];
}
#pragma mark - 设置启动图片
- (void)setupLaunchImage {
    if (iPhone_3_5_inch) {
        self.bgImageView.image = [UIImage imageNamed:@"LaunchImage"];
    } else if (iPhone_4_0_inch) {
        self.bgImageView.image = [UIImage imageNamed:@"LaunchImage-568h"];
    } else if (iPhone_4_7_inch) {
        self.bgImageView.image = [UIImage imageNamed:@"LaunchImage-800-667h"];
    } else if (iPhone_5_5_inch) {
        self.bgImageView.image = [UIImage imageNamed:@"LaunchImage-800-Portrait-736h"];
    }
}
#pragma mark - 查看广告
- (void)tap {
    NSURL *url = [NSURL URLWithString:_adItem.url];
    if ([[UIApplication sharedApplication] canOpenURL:url]) {
        [_timer invalidate];
        [[UIApplication sharedApplication] openURL:url];
    }
}
#pragma mark - 获取广告信息
- (void)getAdInfo {
    NSMutableDictionary *parameters = [NSMutableDictionary dictionary];
    parameters[@"ad"] = @"self.BS0315.iphone.feed.16090609572239";
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    [manager GET:@"http://dspsdk.spriteapp.com/get" parameters:parameters progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        NSDictionary *resultDict = responseObject[@"body"][@"data"];
        NSDictionary *adDict = resultDict[resultDict.allKeys.firstObject];
        _adItem = [BSAdItem mj_objectWithKeyValues:adDict];
        //加载图片
        [self.imageView sd_setImageWithURL:[NSURL URLWithString:_adItem.pic]];
        //设置图片的位置
        self.imageView.frame = CGRectMake(0, 0, KScreenW, KScreenH * 0.7);
        //画圆形动画
        [self drawRedCircle];
        [self setupTimer];
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        BSLog(@"error = %@",error);
    }];
}
#pragma mark - 画圆动画
- (void)drawRedCircle {
    UIBezierPath *bezier = [UIBezierPath bezierPath];
    [bezier addArcWithCenter:CGPointMake(_adTimeBtn.bs_width * 0.5, _adTimeBtn.bs_height * 0.5) radius:_adTimeBtn.bs_width * 0.5 startAngle:- M_PI_2 endAngle:3 * M_PI_2 clockwise:YES];
    CAShapeLayer *layer = [CAShapeLayer layer];
    layer.path = bezier.CGPath;
    layer.fillColor = [UIColor clearColor].CGColor;
    layer.strokeColor = BSRGBColor(255, 46, 92, 1).CGColor;
    layer.lineWidth = 2;
    layer.frame = _adTimeBtn.bounds;
    [_adTimeBtn.layer addSublayer:layer];
    _layer = layer;
    CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"strokeEnd"];
    animation.duration = BSAdTimeInterval;
    animation.fromValue = @0;
    animation.toValue = @1;
    [_layer addAnimation:animation forKey:@"key"];
}
#pragma mark - 设置计时器
- (void)setupTimer {
    _second = BSAdTimeInterval;
    [_adTimeBtn setTitle:[NSString stringWithFormat:@"%zds",_second] forState:UIControlStateNormal];
    _timer = [NSTimer scheduledTimerWithTimeInterval:1.0 repeats:YES block:^(NSTimer * _Nonnull timer) {
        _second --;
        [_adTimeBtn setTitle:[NSString stringWithFormat:@"%zds",_second] forState:UIControlStateNormal];
        if (_second == 0) {
            [self jumpAd];
        }
    }];
}
#pragma mark - 跳过广告按钮的点击
- (IBAction)jumpAd {
    [_timer invalidate];
    [UIApplication sharedApplication].keyWindow.rootViewController = [BSTabBarController new];
}
- (void)dealloc {
    BSLogFunc
    _layer = nil;
    [_timer invalidate];
    _timer = nil;
}


@end
